const que = require('images');
const { con, Result } = require('./common');
const fs = require('fs'),
    mime = require('mime-types'),
    path = require('path');
let Image = async (file) => {
    let filename = `${(new Date().getTime()) + Math.round(Math.random() * 10000)}.jpg`
    const reader = fs.createReadStream(`../../..${file.path}`)
    const upStream = fs.createWriteStream(`./images2/${filename}`)
    reader.pipe(upStream)
    upStream.once('close',function(){
                que(`./images2/${filename}`).save(`./images/${filename}`,{quality:20})
    })
    return filename;
}
let Images = async (files) => {
    let imagesname = []
    for (let file in files) {
        let fileext = `${files[file].type}`.split('/')
        let filename = `${(new Date().getTime()) + Math.round(Math.random() * 10000)}.jpg`
        const reader =	fs.createReadStream(`../../..${files[file].path}`)
        const upStream = fs.createWriteStream(`./images2/${filename}`)
        reader.pipe(upStream)
	upStream.once('close',function(){
		que(`./images2/${filename}`).save(`./images/${filename}`,{quality:20})
	})
        imagesname.push(`${filename}`)
    }
    return imagesname;
}
let Image_get = async () => {
    fs.createReadStream('../images/01.jpg').pipe();
}
let Image_rm = async (filename) => {
    try {
        fs.unlinkSync(filename);
    } catch (e) {
        log(e.message);
    }
}
let Image_mv = async (filename) => {
    let oldname = `./images/${filename}`
    let newname = `./images_temp/${filename}`
    try {
        fs.renameSync(oldname, newname)
    } catch (err) {
        console.log(err.message)
    }
}
module.exports = { Image_get, Image_rm, Image_mv, Image, Images };
